#! /usr/bin/env python
# coding: utf-8
import os
import sys

from waflib import Logs

WAF_VERSION = "1.6.3"

APPNAME = "Steel"
VERSION = "0.0.1 tech1"

top = '.'
out = 'bin'

external_tools = 'waf_modules'

# source files
project_source = """
    ./src/scripts/script.cpp
    ./src/scripts/script_manager.cpp
    ./src/gui/control.cpp
    ./src/gui/core.cpp
    ./src/gui/font.cpp
    ./src/ine.cpp
    ./src/system.c
    ./src/io_file.cpp
    ./src/log.cpp
    ./src/window_manager.cpp
    ./src/math_ext.cpp
    ./src/map.cpp
    ./src/camera.cpp
    ./src/config.cpp
    ./src/texture.cpp
    ./src/image.cpp
    ./src/listener.cpp
    ./src/structures.cpp
    ./src/opengl_shorthands.cpp
""".split()

# Not finished
#    ./src/gui/window.cpp
#    ./src/gui/button.cpp
#    ./src/gui/edit.cpp
#    ./src/gui/list.cpp


# ./src/sqlite/sqlite3.c
# ./src/sqlite/shell.c
# ./src/database.cpp

#
# Setting options
#
def options(opt):
    opt.load('compiler_cc compiler_cxx', download = True)
    opt.load('boost python', tooldir=external_tools)

#
# Configuration
#
def configure(conf):
    conf.load('compiler_cc compiler_cxx', download = True)
    conf.load('boost python', tooldir=external_tools)

    # Dependencies
    # Boost Python
    conf.env.CXXFLAGS_BOOST = ['-DBOOST_ALL_NO_LIB=1', '-DBOOST_PYTHON_SOURCE', '-DBOOST_PYTHON_STATIC_LIB']
    conf.check_boost(lib='python', mt=True, static=True)

    # MinGW
#    conf.env.append_unique('CXXFLAGS', '-static-libgcc -static-libstdc++')


    # FreeImage
    #conf.check_freeimage()

    # Python
    conf.check_python_headers()

    # FreeType2
    # search freetype dir
    path_freetype = conf.find_file(
        'freetype.h', 
        ['/usr/include/freetype2/freetype', 
        'c:\\mingw\\include\\freetype2\\freetype',
        'c:\\mingw\\include\\freetype']) 
    #TODO(hacenator): Add configuration parameter 'freetype2_path'
    if not path_freetype:
        conf.fatal("Freetype2 not found")
    else:
        path_freetype = os.path.abspath(os.path.dirname(path_freetype) + '/..')
    
    #conf.env['INCLUDES_FREETYPE'] = 'freetype2/'
    conf.env['INCLUDES_FREETYPE'] = path_freetype
#    print path_freetype
#    conf.check_cc(header_name="freetype.h")
    conf.check(header_name="ft2build.h", compile_mode='c', includes=path_freetype)
    conf.check_cc(lib="freetype", mandatory=1)

    # OpenGL
    conf.check_cc(header_name="GL/glew.h")

    if sys.platform == "win32":
        conf.check_cc(lib="opengl32", mandatory=1)
        conf.check_cc(lib="glu32", mandatory=1)
        conf.check_cc(lib="glew32", mandatory=1)
    else:
        # ubuntu 11.04
        conf.check_cc(lib="GL", mandatory=1)
        conf.check_cc(lib="GLU", mandatory=1)
        conf.check_cc(lib="GLEW", mandatory=1)

    # jpeg
    conf.check_cc(lib="jpeg", mandatory=1)
    conf.check_cc(header_name=["stdio.h", "jpeglib.h"], lib="jpeg")

    # png
    conf.check_cc(lib="png", mandatory=1)
    conf.check_cc(header_name="png.h", lib="png")

    # gcc
    #conf.check_cc(lib="gcc", mandatory=1)

    # zlib
    conf.check_cc(lib="z", mandatory=1)
    conf.check_cc(header_name="zlib.h", lib="z")

    # SDL
    if sys.platform == "win32":
        conf.env.CXXFLAGS_SDL = ['-D_GNU_SOURCE=1', '-Dmain=SDL_main', '-mwindows']
        conf.check_cc(lib="SDL", mandatory=1)
        conf.check_cc(lib="SDLmain", mandatory=1)
        conf.check_cc(header_name="SDL.h", fragment="int main(int argc, char * argv[]) {}\n", lib="SDL")
        conf.env.LIB_SDL=['mingw32', 'SDLmain', 'SDL']
    else:
        # ubuntu 11.04
        conf.check_cfg(path='sdl-config', args='--cflags --libs', package='', userlib_store='SDL')
        conf.check_cc(lib="SDL")

    # variants
    conf.setenv('debug', env=conf.env.derive())
    conf.env.CFLAGS = conf.env.CXXFLAGS = ['-g', '-DDEBUG']

    conf.setenv('release', env=conf.env.derive())
    conf.env.CFLAGS = conf.env.CXXFLAGS = ['-O3', '-DRELEASE']

#
# Build
#
def build(bld):
    #bld.msg("Building '%s' variant " % bld.variant, "OK");

    use_list = []
    defines = ['_GNU_SOURCE=1', 'BOOST_ALL_NO_LIB=1', 'BOOST_PYTHON_SOURCE', 'BOOST_PYTHON_STATIC_LIB']
    if sys.platform == "win32":
        use_list = ['BOOST', 'PYEMBED', 'SDL', 'OPENGL32', 'GLU32', 'GLEW32', 'FREETYPE', 'Z', 'JPEG', 'PNG']
        defines.append('main=SDL_main')
    else:
        # ubuntu 11.04
        use_list = ['BOOST', 'PYEMBED', 'SDL', 'GL', 'GLU', 'GLEW', 'FREETYPE', 'Z', 'JPEG', 'PNG']

    bld(
        features = 'cxx cprogram',
        source = project_source,
        includes = ['./include'],
        target = 'steel',
        defines = defines,
        use = use_list,
        linkflags = ['-static-libgcc', '-static-libstdc++'],
    )

    #bld.logger = Logs.make_logger('build.log', 'build') # just to get a clean output

# using absolute path instead of relative
# this is solve gdb problem on win32 platform
import waflib.Tools.c as fooc
class c(fooc.c):
    run_str = '${CC} ${ARCH_ST:ARCH} ${CFLAGS} ${CPPFLAGS} ${FRAMEWORKPATH_ST:FRAMEWORKPATH} ${CPPPATH_ST:INCPATHS} ${DEFINES_ST:DEFINES} ${CC_SRC_F}${SRC[0].abspath()} ${CC_TGT_F}${TGT}'

import waflib.Tools.cxx as fooxx
class cxx(fooxx.cxx):
    run_str = '${CXX} ${CXXFLAGS} ${CPPFLAGS} ${FRAMEWORKPATH_ST:FRAMEWORKPATH} ${CPPPATH_ST:INCPATHS} ${DEFINES_ST:DEFINES} ${CXX_SRC_F}${SRC[0].abspath()} ${CXX_TGT_F}${TGT}'

# variants
from waflib.Build import BuildContext, CleanContext, InstallContext, UninstallContext

for x in 'debug release'.split():
    for y in (BuildContext, CleanContext, InstallContext, UninstallContext):
        name = y.__name__.replace('Context','').lower()
        class tmp(y):
            cmd = name + '_' + x
            variant = x
